<template>
  <div ref="wrapEl">
    <AdTable @register="registerTable">
      <template #toolbar>
        <Authority :value="'Root.Admin.Systems.Dict.CreateDictEntry'">
          <a-button type="primary" @click="handleCreate">
            <Icon icon="ant-design:plus-circle-outlined" />新增
          </a-button>
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                auth: '', // 权限控制
                tooltip: '编辑',
                ifShow: (_action) => {
                  return true;
                },
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                auth: '', // 权限控制
                ifShow: (_action) => {
                  return true;
                },
                popConfirm: {
                  title: '确认删除？',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </AdTable>
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref, watch } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import { useLoading } from '@/components/Loading';
  import { Authority } from '@/components/Authority';
  import { FormModeEnum } from '@/enums/formEnum';
  import { AjaxResultTypeEnum } from '@/enums/ajaxResultTypeEnum';
  import { AdTable, useAdTable, TableAction } from '@/components/AdTable';
  import { dictEntryColumns } from './dict.data';
  import { ReadDictEntry, CreateDictEntry, UpdateDictEntry, DeleteDictEntry } from './dict.api';
  import { FilterGroup, FilterRule } from '@/utils/common/types';
  import { FilterOperateEnum } from '@/enums/filterOperateEnum';
  import Icon from '@/components/Icon/Icon.vue';

  defineOptions({ name: 'DictManagePage' });
  const { createMessage } = useMessage();

  const wrapEl = ref<ElRef>(null);
  const [openWrapLoading, closeWrapLoading] = useLoading({
    target: wrapEl,
    props: {
      tip: '加载中...',
      absolute: true,
    },
  });

  const props = defineProps({
    dictId: { type: String, default: '' },
  });
  const [registerTable, tableMethods] = useAdTable({
    // 表标题呈现器
    title: '字典值',
    // 表格数据接口请求对象
    api: ReadDictEntry,
    // 详情表单抬头
    detailForm: {
      // 表单标识
      formIndex: 'dictentry',
      // 主键字段
      keyField: 'Id',
      title: '字典项详情',
      createApi: CreateDictEntry,
      updateApi: UpdateDictEntry,
      deleteApi: DeleteDictEntry,
    },
    columns: dictEntryColumns,
  });

  watch(
    () => props.dictId,
    (newValue, oldValue) => {
      if (newValue != null) {
        const group = new FilterGroup();
        group.Rules.push(new FilterRule('DictId', `${newValue}`, FilterOperateEnum.Equal));
        tableMethods.setProps({ searchInfo: group });
      } else {
        tableMethods.setProps({ searchInfo: undefined });
      }
      tableMethods.reload();
    },
    {
      deep: false,
      immediate: false,
    },
  );

  async function handleCreate() {
    tableMethods.openDetailForm(FormModeEnum.Add, { DictId: props.dictId });
  }

  async function handleEdit(record: Recordable) {
    tableMethods.openDetailForm(FormModeEnum.Edit, record);
  }

  /** 处理删除 */
  async function handleDelete(record: Recordable) {
    const ajaxResult = await DeleteDictEntry([record.Id]);
    if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
      if (ajaxResult.Content) {
        createMessage.success(ajaxResult.Content);
      }
      tableMethods.reload();
    }
  }
</script>
